// pages/zhanbo/detail.js
const http = require('../../utils/http.js');
import {
	refreshPage
} from '../../utils/common'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: null,
		isCollect: 0,
		collect_num: 0,
		infoData: {},
		vote_num: 0,
		list: [],
		isLoading: true,

		isGgShow: true,
		pageCurrent: 1,
		isListMore: true,

		inf_data: [],
		screen_data: null
	},
	getUserInfo() {
		http.post('api/user/info').then(res => {
			if (res.code == 1) {
				wx.setStorageSync('userInfo', res.data);
				this.setData({
					vote_num: res.data.use_vote_num
				})
			}

		})
	},
	getData() {
		var that = this;
		http.post('api/works/detail', {
			id: that.data.id
		}).then(res => {
			if (res.code == 1) {
				that.setData({
					infoData: res.data,
					isCollect: res.data.collect,
					collect_num: res.data.numall.collect_num
				})
			}

		})
		that.getUserInfo();
		that.getNotice();
		that.getScreen();
		that.getList(1);
	},
	getGg(name) {
		return http.post('api/advert/get_advert', {
			en: name,
			li: 0
		})
	},
	// 横幅广告
	getNotice() {
		this.getGg('ad_inf_banner').then(res => {
			console.log(res)
			this.setData({
				inf_data: res.data
			})
		})
	},
	// 中屏广告
	getScreen() {
		this.getGg('ad_tab_screen').then(res => {
			console.log(res)
			this.setData({
				screen_data: res.data[0]
			})
		})
	},
	getList(isFirst) {
		var that = this;
		var data = {
			pg: isFirst ? 1 : this.data.pageCurrent
		}
		http.post('api/works/list_like', data, 1).then(res => {
			var pageCurrent = that.data.pageCurrent;
			if (res.data.length > 0) {
				that.setData({
					pageCurrent: isFirst ? 2 : pageCurrent + 1,
					list: isFirst ? res.data : that.data.list.concat(res.data),
					isLoading: false,
					isListMore: true
				})
			} else {
				setTimeout(function() {
					that.setData({
						isListMore: false,
						isLoading: false,
						isnoMore: isFirst ? false : true
					})
				}, 1000)

			}
		})
	},
	// 收藏
	changeCollect() {
		var isCollect = this.data.isCollect;
		http.post('api/user/ac_collect', {
			id: parseInt(this.data.id),
			t: isCollect ? 'del' : 'ins'
		}).then(res => {
			if (res.code == 1) {
				wx.showToast({
					title: isCollect ? '取消收藏成功' : '收藏成功',
					icon: 'none'
				})
				this.setData({
					isCollect: !isCollect,
					collect_num: isCollect ? this.data.collect_num - 1 : this.data.collect_num +
						1
				})
				console.log(this.data.collect_num)
			}
		})

	},
	//投票
	onAcVote() {
		http.post('api/user/ac_vote', {
			id: this.data.id
		}).then(res => {
			if (res.code === 1) {
				wx.showToast({
					title: '投票成功！'
				})
				this.getData();
			} else {
				// wx.showToast({
				//   title: res.msg,
				//   icon: 'none'
				// })
				// refreshPage()
			}
		})
	},
	//关闭广告
	onCloseGg() {
		this.setData({
			isGgShow: false
		})
	},
	goPath(e) {
		wx.navigateTo({
			url: e.currentTarget.dataset.url,
		})
	},
	goGuanggao(e){
		var type = e.currentTarget.dataset.type;
		var url = e.currentTarget.dataset.url;
		if (type == 1) {
			// 小程序
		} else if (type == 2) {
			// 本地
			return wx.navigateTo({
				url
			});
		} else if (type == 3) {
			// H5
			return wx.navigateTo({
				url: '/pages/webview/webview?url=' + url
			});
		}
	},
	acHelp(id){
		http.post('api/user/ac_help',{id}).then(res => {
			if(res.code == 1){
				wx.showToast({
					title: '助力成功~',
					icon: 'none'
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			id: options.id
		})
		this.getData()
		if(options.shareUserId){
			this.acHelp(options.shareUserId)
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	onReachBottom() {
		if (this.data.isListMore) {
			this.setData({
				isLoading: true
			})
			this.getList();
		}

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {
		console.log(wx.getStorageSync('userInfo'))
		return {
			path: '/pages/zhanbo/detail?id='+this.data.id+'&shareUserId=' + wx.getStorageSync('userInfo').user_id
		}
	}
})